
let sql = {};

/**
 * @description: 小程序 mysql写表方式埋点
 * @param {object} context 方法对象
 * @param {object} obj 埋点数据
 * @return {object} 结果
 */
sql.insert = async (context, obj)=>{
  try{

    if (!context || !obj.projectCode || !obj.userid) {
      return {
        success: false,
        data: {},
        errorCode: "422",
        errorMessage: 'projectCode or userid or mixNick must be needed!'
      }
    }

    //  备注：  修改mysql判断
    if (typeof context.mfutil.db.mysql.query != "function") {
      return {
        success: false,
        data: {},
        errorCode: "501",
        errorMessage: 'No mysql instance exists!'
      }
    }

    // 小程序埋点写入数据库
    let sql = `insert into tracking_message_record (data, gmt_create, gmt_modified ) VALUES ('${JSON.stringify(obj)}', now(), now())`;

    let res = await context.mfutil.db.mysql.query(sql);
    return {
      success: true,
      data: res,
      errorCode: "",
      errorMessage: ""
    }
  }catch(e){
    return {
      success: false,
      data: {},
      errorCode: "999",
      errorMessage: JSON.stringify(e)
    }
  }
}

module.exports = exports.sql = sql;